package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.common.utils.FtpClient;
import com.jiangyao.common.utils.StringUtils;
import com.jiangyao.dao.ILecturerDao;
import com.jiangyao.dao.ISubjectDao;
import com.jiangyao.model.dto.backend.LecturerDTO;
import com.jiangyao.model.dto.backend.LecturerListPageDTO;
import com.jiangyao.model.entity.Lecturer;
import com.jiangyao.model.entity.Subject;
import com.jiangyao.model.vo.backend.LecturerVO;
import com.jiangyao.service.LecturerService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-08-18
 */
@Service
public class LecturerServiceImpl extends ServiceImpl<ILecturerDao, Lecturer> implements LecturerService {

	@Autowired
	private DozerBeanMapper dozerBeanMapper;

	@Autowired
	private ISubjectDao subjectDao;

	@Override
	public Page<Lecturer> getListPage(LecturerListPageDTO listPageDTO) {
		QueryWrapper<Lecturer> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotBlank(listPageDTO.getLecturerName())) {
			queryWrapper.like("lecturer_name", listPageDTO.getLecturerName());
		}
		if (StringUtils.isNotBlank(listPageDTO.getLecturerPhone())) {
			queryWrapper.like("lecturer_phone", listPageDTO.getLecturerPhone());
		}
		if (listPageDTO.getStatus() != null && listPageDTO.getStatus() != -1) {
			queryWrapper.eq("status", listPageDTO.getStatus());
		}
		queryWrapper.select("lecturer_id", "lecturer_name", "lecturer_phone", "create_time", "status");
		queryWrapper.orderByDesc("lecturer_id");
		Page<Lecturer> page = new Page<>(listPageDTO.getPageNo(), listPageDTO.getPageSize());
		this.page(page, queryWrapper);
		return page;
	}

	@Override
	public void add(LecturerDTO lecturerDTO) {
		Lecturer lecturer = getOneByName(lecturerDTO);
		if (lecturer != null) {
			throw new ServerException(100, "讲师已存在");
		}
		lecturer = dozerBeanMapper.map(lecturerDTO, Lecturer.class);
		lecturer.setLecturerId(null);
		lecturer.setCreateTime(new Date());
		lecturer.setStatus(1);
		this.save(lecturer);
	}

	@Override
	public void edit(LecturerDTO lecturerDTO) {
		Lecturer lecturerDB = this.getById(lecturerDTO.getLecturerId());
		if (lecturerDB == null || lecturerDB.getStatus() != 1) {
			throw new ServerException(100, "讲师不存在");
		}
		if (!lecturerDB.getLecturerName().equals(lecturerDTO.getLecturerName())) {
			Lecturer lecturer = getOneByName(lecturerDTO);
			if (lecturer != null) {
				throw new ServerException(100, "讲师已存在");
			}
		}
		UpdateWrapper<Lecturer> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("lecturer_id", lecturerDTO.getLecturerId());
		updateWrapper.set("lecturer_name", lecturerDTO.getLecturerName());
		updateWrapper.set("lecturer_phone", lecturerDTO.getLecturerPhone());
		updateWrapper.set("lecturer_photo", lecturerDTO.getLecturerPhoto());
		updateWrapper.set("lecturer_desc", lecturerDTO.getLecturerDesc());
		this.update(updateWrapper);
	}

	@Override
	public LecturerVO getDetail(LecturerDTO lecturerDTO) {
		Lecturer lecturerDB = this.getById(lecturerDTO.getLecturerId());
		if (lecturerDB == null || lecturerDB.getStatus() != 1) {
			throw new ServerException(100, "讲师不存在");
		}
		LecturerVO lecturerVO = dozerBeanMapper.map(lecturerDB, LecturerVO.class);
		lecturerVO.setLecturerPhotoFull(FtpClient.fullFtpUrl(lecturerVO.getLecturerPhoto()));
		return lecturerVO;
	}

	@Override
	public void editStatus(LecturerDTO lecturerDTO) {
		Lecturer lecturerDB = this.getById(lecturerDTO.getLecturerId());
		if (lecturerDB == null || lecturerDB.getStatus() != 1) {
			throw new ServerException(100, "讲师不存在");
		}
		// 检查是否可以禁用
		checkDelete(lecturerDB);
		// 修改状态
		UpdateWrapper<Lecturer> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("lecturer_id", lecturerDB.getLecturerId());
		updateWrapper.set("status", lecturerDTO.getStatus());
		this.update(updateWrapper);
	}

	@Override
	public List<Lecturer> listForSelect(LecturerDTO lecturerDTO) {
		QueryWrapper<Lecturer> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotBlank(lecturerDTO.getLecturerName())) {
			queryWrapper.like("lecturer_name", lecturerDTO.getLecturerName());
		}
		queryWrapper.eq("status", 1);
		queryWrapper.select("lecturer_id", "lecturer_name", "lecturer_phone");
		queryWrapper.orderByDesc("lecturer_id");
		queryWrapper.last(" LIMIT 50");
		return this.list(queryWrapper);
	}

	/**
	 * 根据名称获取讲师
	 *
	 * @param lecturerDTO 讲师DTO
	 * @return 结果
	 */
	private Lecturer getOneByName(LecturerDTO lecturerDTO) {
		if (StringUtils.isBlank(lecturerDTO.getLecturerName())) {
			return null;
		}
		QueryWrapper<Lecturer> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("lecturer_name", lecturerDTO.getLecturerName());
		queryWrapper.last(" LIMIT 1");
		return this.getOne(queryWrapper);
	}

	/**
	 * 检查讲师是否删除
	 *
	 * @param lecturer 讲师
	 */
	private void checkDelete(Lecturer lecturer) {
		QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("lecturer_admin", lecturer.getLecturerId());
		queryWrapper.eq("status", 1);
		Integer count = subjectDao.selectCount(queryWrapper);
		if (count != null && count > 0) {
			throw new ServerException(100, "该讲师还绑定了课程，不能删除");
		}
	}
}
